package Chuong04;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Bai01 extends JFrame implements ActionListener{
	
	Label lblSo1 = new Label("Số thứ nhất:");
	Label lblSo2 = new Label("Số thứ hai:");
	Label lblKQ = new Label("Kết quả:");
	
	JTextField txtSo1 = new JTextField(20);
	JTextField txtSo2 = new JTextField(20);
	JTextField txtKQ = new JTextField(20);
	
	JButton btnKQ = new JButton("Tổng");
	JButton btnXoa = new JButton("Xóa");
	JButton btnThoat = new JButton("Thoát");
	
	Panel p,p1,p2,p3,p4,p5;
	
	public Bai01() {
		super("Bài 01 - Chương 04");
		btnKQ.addActionListener(this);
		btnXoa.addActionListener(this);
		btnThoat.addActionListener(this);
		setSize(400, 300);
		txtKQ.setEnabled(false);
		
		p1 = new Panel();
		p1.setLayout(new GridLayout(1,2));
		p1.add(lblSo1, BorderLayout.WEST);
		p1.add(txtSo1, BorderLayout.EAST);
		
		p2 = new Panel();
		p2.setLayout(new GridLayout(1,2));
		p2.add(lblSo2, BorderLayout.WEST);
		p2.add(txtSo2, BorderLayout.EAST);
		
		p3 = new Panel();
		p3.setLayout(new GridLayout(1,2));
		p3.add(lblKQ, BorderLayout.WEST);
		p3.add(txtKQ, BorderLayout.EAST);
		
		p4 = new Panel();
		p4.setLayout(new GridLayout(1,2));
		p4.add(btnKQ, BorderLayout.CENTER);
		p4.add(btnXoa, BorderLayout.CENTER);
		
		p5 = new Panel();
		p5.setLayout(new GridLayout(1,1));
		p5.add(btnThoat, BorderLayout.CENTER);
		
		p = new Panel();
		p.setLayout(new GridLayout(5,1));
		p.add(p1);
		p.add(p2);
		p.add(p3);
		p.add(p4);
		p.add(p5);
		add(p);
	}
	
	public static void main(String[] args) {
		Bai01 frm = new Bai01();
		frm.setSize(400,200);
		frm.setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if(arg0.getSource()==btnThoat){
			System.exit(0);
		}
		
		if(arg0.getSource()==btnKQ){
			try{
				int so1 = Integer.parseInt(txtSo1.getText());
				int so2 = Integer.parseInt(txtSo2.getText());
				int kq = so1 + so2;
				txtKQ.setText(String.valueOf(kq));
			}
			catch (Exception e) {
				txtKQ.setText("Lỗi nhập liệu!");
				JOptionPane.showMessageDialog(this,"Lỗi nhập liệu!","Lỗi",JOptionPane.ERROR_MESSAGE);
			}
			
		}
		
		if(arg0.getSource()== btnXoa){
			txtSo1.setText("");
			txtSo2.setText("");
			txtKQ.setText("");
		}
	}

}
